package com.guandi.geekbrowser.primary.repository;

import com.guandi.geekbrowser.primary.entity.Details;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Repository
public interface DetailsRepository extends JpaRepository<Details,Long>, JpaSpecificationExecutor<Details> {
    List<Details> findByTabName(String name);

    List<Details> findByTabNameAndUserId(String tabName, String userId);

    Page<Details> findAll(Specification<Details> specification, Pageable pageable);

    @Query(nativeQuery=true, value = "select * from `details` d where d.tab_name=?1 and d.user_id = ?2 and (d.password is null or d.password='') AND d.register = false ")
    List<Details> findByTabNameAndUserIdQuery(String tabName, String UserId);

    @Query(nativeQuery=true, value = "select * from `details` d where d.tab_name=?1 and d.user_id = ?2 AND d.country = ?3 and (d.password is null or d.password='') AND d.register = false")
    List<Details> findByTabNameAndUserIdAndCountryQuery(String tabName, String UserId,String country);

    Optional<Details> findById(Long detailId);

    Details findByIdAndUserId(Long detailId,String id);

    Details findByIdAndUserIdAndCountry(Long detailId,String id,String country);

    List<Details> findByUserIdAndCountry(String id,String country);

    List<Details> findByUserIdAndCountryAndType(String id,String country,Integer type);

    @Query(nativeQuery=true, value = "select * from `details` d where d.user_id = ?1 and d.country = ?2 and (d.password is not null or d.password!='') order by rand() limit 5 ")
    List<Details> findByUserIdAndCountryQuery(String id,String country);

    @Query(value = "from Details d where d.id=?1 and d.userId = ?2")
    Details findByIdAndUserIdQuery(Long detailId,String id);

    @Transactional
    Integer deleteAllByTabNameAndUserId(String tabName,String userId);

    @Transactional
    Integer deleteAllByTabNameAndUserIdAndCountry(String tabName,String userId,String country);

    @Transactional
    @Query(value = "delete FROM Details d where d.id=?1 and d.userId=?2")
    @Modifying
    Integer deleteByIdAndUserId(Long id,String userId);

    @Query(value = "UPDATE Details d SET d.notes = ?1 where d.id = ?2 and d.userId = ?3")
    @Modifying
    @Transactional
    void updateOne(String name,Long id,String userId);

    @Query(value = "UPDATE Details d SET d.register = ?2 where d.id = ?1")
    @Modifying
    @Transactional
    Integer updateRegister(Long id,boolean register);

    @Query(value = "UPDATE Details d SET d.password = ?2 where d.id = ?1 and d.userId =?3")
    @Modifying
    @Transactional
    Integer updatePassword(Long id,String password,String uid);

    @Query(value = "UPDATE Details d SET d.pcCookie = ?2 where d.id = ?1 and d.userId =?3")
    @Modifying
    @Transactional
    Integer updatePcCookie(Long id,String pcCookie,String uid);

    List<Details> findByRegIp(String ip);

    List<Details> findByRegIpAndCountry(String ip,String country);

    List<Details> findByUserId(String userId);



}
